package com.tiantang.shiro.helloworld.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @description：功能描述	第二个realm（测试多realm）
 * @author： liujinkun
 * 创建时间： 2018年1月20日
 * @version：1.0
 * Copyright (c) 2018年 京东方科技集团股份有限公司
 */
public class SecondShiroRealm extends AuthenticatingRealm{
	
	private static Logger logger=LoggerFactory.getLogger(SecondShiroRealm.class);

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		logger.info("=========>>>>>>>>进入第二个Realm");
		UsernamePasswordToken uToken=(UsernamePasswordToken)token;
		String username=uToken.getUsername();
		
		System.out.println("从数据中获取用户信息");
		if("unknown".equals(username)){
			throw new UnknownAccountException("用户不存在");
		}
		
		if("monster".equals(username)){
			throw new LockedAccountException("用户被锁定");
		}
		
		Object principal=username;
		
		Object credentials="40561397f0540e3a2d0273ed9ec3bbc2738a3385";
		
		String realmName=getName();
		logger.info("当前realm的name为"+realmName);
		
		//盐值
		ByteSource credentialsSalt=ByteSource.Util.bytes(username);
		SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
		
		return info;
	}
	
}
